<?php
	include 'ZionHeader.inc';
?>

<html>
<body>

	<?php
		$name = $_SESSION['UName'];
		$password = $_POST['password'];
		
		
		//Need to compare $name with username of announcement
	?>


<?php
$Make = $_POST['make'];
$Model = $_POST['model'];
$Year = $_POST['year'];
$Price = $_POST['price'];
$Vin = $_POST['vin'];
$Cylinder = $_POST['cylinder'];
$Drive = $_POST['drive'];
$Color = $_POST['color'];
$Description = $_POST['description'];
$Mileage = $_POST['mileage'];
$NumOfDoors = $_POST['numberofdoors'];

include 'DatabaseConnect.inc';

/*
//We want to grab the image file and place it into the table called imageTable 
//and then place a name of it into announcements.

if ($_FILES['photo']['size'] > 0)
{
	$imgName = $_FILES['photo']['name'];
        $tmpName  = $_FILES['photo']['tmp_name'];
        $fSize = $_FILES['photo']['size'];
        $fType = $_FILES['photo']['type'];

        $fp      = fopen($tmpName, 'r');
        $imgData = fread($fp, filesize($tmpName));
        $imgData = addslashes($imgData);
        fclose($fp);

		
        if(!get_magic_quotes_gpc())
        {
           $imgName = addslashes($imgName);
        }

	// The getimagesize() function will determine the size of any given image file 
	// and return the dimensions along with the file type and a height/width text string 
	// to be used inside a normal HTML IMG tag and the correspondant HTTP content type.
	$imgSize = getimagesize($_FILES['photo']['tmp_name']);
	include 'DatabaseConnect.inc';
	// query the database to see if there is an image which matches
	$query = "select count(*) from imageTable where user = '$name' and imgName = '$imgName'";

	$result = mysql_query($query);
	if(!$result){
		echo "Cannot execute query<br>";
		exit;
	}
	
	$row = mysql_fetch_row( $result );
	$count = $row[0];

	if ( $count > 0 )   {
		printf("You have already uploaded an image with the same filename.<br>");
	}
	else{
		$query ="INSERT INTO imageTable (id,user,imgData,imgName,imgSize,fileSize,fileType)". 
			" VALUES ('','$name','$imgData','$imgName','$imgSize[3]','$fSize','$fType',)";

		if(!mysql_query($query)){
			printf("Upload failed: %s<br>", mysql_error($mysql));
			exit;
		}
	}		
}
*/

$Photo = $_POST['photo'];



$query = "INSERT INTO Vehicle (VIN,Price,Cylinder,Drive,Color,Description,Mileage,Photo,Make,Model,Year,NoDoors ) Values ('$Vin', '$Price', '$Cylinder', '$Drive', '$Color', '$Description', '$Mileage', '$Photo', '$Make', '$Model', '$Year','$NumOfDoors')";

mysql_query($query);

$query = "SELECT * FROM Vehicle WHERE VIN = '$Vin'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$vID = $row['ID'];

//We want to grab the image file and place it into the table called imageTable 
//and then place a name of it into announcements.

if ($_FILES['photo']['size'] > 0)
{
	$imgName = $_FILES['photo']['name'];
        $tmpName  = $_FILES['photo']['tmp_name'];
        $fSize = $_FILES['photo']['size'];
        $fType = $_FILES['photo']['type'];

        $fp      = fopen($tmpName, 'r');
        $imgData = fread($fp, filesize($tmpName));
        $imgData = addslashes($imgData);
        fclose($fp);

		
        if(!get_magic_quotes_gpc())
        {
           $imgName = addslashes($imgName);
        }

	// The getimagesize() function will determine the size of any given image file 
	// and return the dimensions along with the file type and a height/width text string 
	// to be used inside a normal HTML IMG tag and the correspondant HTTP content type.
	$imgSize = getimagesize($_FILES['photo']['tmp_name']);
	//include 'DatabaseConnect.inc';
	// query the database to see if there is an image which matches
	$query = "select count(*) from imageTable where user = '$name' and imgName = '$imgName'";

	$result = mysql_query($query);
	if(!$result){
		echo "Cannot execute query<br>";
		exit;
	}
	
	$row = mysql_fetch_row( $result );
	$count = $row[0];

	if ( $count > 0 )   {
		printf("You have already uploaded an image with the same filename.<br>");
	}
	else{
		$query ="INSERT INTO imageTable (id,user,imgData,imgName,imgSize,fileSize,fileType,VID)". 
			" VALUES ('','$name','$imgData','$imgName','$imgSize[3]','$fSize','$fType','$vID')";

		if(!mysql_query($query)){
			printf("Upload failed: %s<br>", mysql_error($mysql));
			exit;
		}
	}		
}



$query = "SELECT * from Vehicle WHERE VIN = '$Vin'";

$result = mysql_query($query);
$row = mysql_fetch_array($result);

$username = $_SESSION['UName'];

$time = date(DATE_RFC822);

$query = "INSERT INTO  Announcement ( Timestamp, vehicle_ID, userID) Values ('$time', '".$row['ID']."', '$username')";

mysql_query($query);

mysql_close();


?>

<h1>Vehicle has been added!</h1>
<br />
<br />
<a href="index.php">Return To Index</a>


</body>
</html>

